package com.gyhy.dumplings.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.gyhy.dumplings.common.utils.JwtUtils;
import com.gyhy.dumplings.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: 光影灰翼
 * @date: 2022-09-25 10:34
 * @version: 1.0
 * @email: 978330688@qq.com
 */
@Slf4j
// @Controller
@RefreshScope
@RestController
@RequestMapping("/oauth")
public class LoginUpdateController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping(value = "/test")
    public R test() throws Exception{
        System.out.println("test success");
        return R.ok().setData("123");
    }

    @PostMapping("/updateLoginState")
    public boolean updateLoginState(@RequestBody JSONObject json ){
        String token= String.valueOf(JwtUtils.decode(json.getString("token")).getBody().get("id"));
        if(redisTemplate.opsForValue().get(token) == null) {
            System.out.println(token+"用户已过期，请重新登录！");
            return false;
        }
        redisTemplate.expire(token, 1, TimeUnit.DAYS);
        System.out.println(token+"用户状态更新成功！");
        return true;
    }
}
